const axios = require("axios");
const cheerio = require("cheerio");
// 获取游侠首页数据入口
async function getYouXiaTitleContent(type) {
  const res = await axios.get("https://www.ali213.net/");
  if (type === "news") {
    return [parsePagenewsList(res.data), parsePageHotTitle(res.data)];
  }
  if (type === "judge") {
    return parsePageGameJudge(res.data);
  }
  if (type === "pre") {
    return parsePageGamePre(res.data);
  }
}

// 获取网页新闻条目数据
function parsePagenewsList(page) {
  let newsList = [];
  const $ = cheerio.load(page);
  $(".news-link-top>li").each(function () {
    const item = {};
    item.title = $("i", this).text();
    item.content = $("a", this).text();
    item.target = $("a", this).attr("href");
    item.time = $("span", this).text();
    newsList.push(item);
  });
  return newsList;
}

// 获取网页新闻标题数据
function parsePageHotTitle(page) {
  let redList = [];
  let textList = [];

  const $ = cheerio.load(page);
  $(".s1-m-li>.rdzx-li .rdzx-top .h3-a a").each(function () {
    const redItem = {};
    redItem.title = $(this).text();
    redItem.target = $(this).attr("href");
    if (redList.length < 4) redList.push(redItem);
  });
  $(".s1-m-li>.rdzx-li .rdzx-top .three-a a").each(function () {
    const textItem = {};
    textItem.title = $(this).text();
    textItem.target = $(this).attr("href");
    if (textList.length < 6) textList.push(textItem);
  });
  return [redList, textList];
}

// 获取游戏测评数据
function parsePageGameJudge(page) {
  let judgeList = [];
  const $ = cheerio.load(page);
  $("#con_yxpc_1 .l-pc-li").each(function () {
    const item = {};
    item.name = $("a", this).attr("title");
    item.link = $("a", this).attr("href");
    item.img = "https:" + $("a img", this).attr("data-original");
    item.introduce = $(".game-name p", this).text();
    item.score = $(".l-pc-score p", this).text().trim();
    item.judge = $(".l-pc-score em", this).text().trim();
    judgeList.push(item);
  });
  return judgeList;
}

// 获取游戏前瞻数据
function parsePageGamePre(page) {
  let gamePreList = [];
  const $ = cheerio.load(page);
  const headItem = {};
  headItem.title = $("#con_yxpc_2 .s1-l2-news-head a").attr("title");
  headItem.link = $("#con_yxpc_2 .s1-l2-news-head a").attr("href");
  headItem.img =
    "https:" + $("#con_yxpc_2 .s1-l2-news-head img").attr("data-original");
  headItem.introduce = $("#con_yxpc_2 .s1-l2-news-head p").text();
  $("#con_yxpc_2 .s1-l2-ul a").each(function () {
    const item = {};
    item.title = $(this).attr("title");
    item.link = $(this).attr("href");
    gamePreList.push(item);
  });
  return [[headItem], gamePreList];
}

module.exports = getYouXiaTitleContent;
